package fantasy;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;

public class GOAuthTestServlet extends HttpServlet {
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();

		GOAuth g = new GOAuth();
		try {
			Key k = new KeyFactory.Builder(GOAuth.class.getSimpleName(), user.getNickname()).getKey();
			g = pm.getObjectById(GOAuth.class, k);
		} catch (JDOObjectNotFoundException e) {
			log("User hasn't done the OAuth dance");
			return;
		}
		
		String ACCESS_TOKEN = g.getAccessToken();
		String TOKEN_SECRET = g.getAccessTokenSecret();

		log("Access Token: " + ACCESS_TOKEN);
		log("Access Token Secret: " + TOKEN_SECRET);
		
		GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
		oauthParameters.setOAuthConsumerKey("dhpollack944.appspot.com");
		oauthParameters.setOAuthConsumerSecret("5sLdLvnBuYa+fpcWdzie9ndr");
		oauthParameters.setOAuthToken(ACCESS_TOKEN);
		oauthParameters.setOAuthTokenSecret(TOKEN_SECRET);


		log("Starting Docs Test");
		DocsService client = new DocsService("dhpollack-dhpollack944-v6");
		try {
			client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
			URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/-/folder");
			DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
			for (DocumentListEntry entry : resultFeed.getEntries()) {
				    log(entry.getTitle().getPlainText());
			}
		} catch (OAuthException e) {
			// TODO Auto-generated catch block
			log("OAuth Error");
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log("Service Exception Error");
			log(e.getResponseBody());
			e.printStackTrace();
		}
		
		log("Starting Spreadsheet Test");
		SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
		URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
		
		try {
			myService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
			SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
			List<SpreadsheetEntry> spreadsheets = feed.getEntries();
			for (int i = 0; i < spreadsheets.size(); i++) {
			  SpreadsheetEntry entry = spreadsheets.get(i);
			  log(entry.getTitle().getPlainText());
			}

		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log(e.getResponseBody());
			e.printStackTrace();
		} catch (OAuthException e) {
			// TODO Auto-generated catch block
			log(e.toString());
			e.printStackTrace();
		}



	}



}
